/**
 *  Copyright (C) 2008-2013  Telosys project org. ( http://www.telosys.org/ )
 *
 *  Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *          http://www.gnu.org/licenses/lgpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.telosys.persistence.metadata;

/**
 * POJO for storage of MetaData about Columns of a Table from a database
 * 
 * NB : For future use (not yet qualified)
 *
 * @author Olivier Laurendeau 
 * 
 */
public class DbColumn {
	
	private String _sTableCatalog; //may be null
	private String _sTableSchema; //may be null
	private String _sTableName;
	private String _sColumnName;
	
	//SQL type from java.sql.Types
	private int _iDataType;
	
	//Data source dependent type name, for a UDT the type name is fully qualified
	private String _sTypeName;
	
	//column size. For char or date types this is the maximum number of characters,
	//for numeric or decimal types this is precision.
	private int _iColumnSize;
	
	private int _iBufferLength; //Is not used
	
	//the number of fractional digits
	private int _iDecimalDigits; 
	
	//Radix (typically either 10 or 2)
	private int _iNumPrecRadix;
	
	/*is NULL allowed. Values :
		* columnNoNulls - might not allow NULL values
	    * columnNullable - definitely allows NULL values
	    * columnNullableUnknown - nullability unknown */
	private int _iNullable;
	
	//comment describing column
	private String _sRemarks; //may be null
	
	//default value
	private String _sColumnDef; //may be null
	
	private int _iSqlDataType; //unused
	
	private int _iSqlDatetimeSub; //unused
	
	//for char types the maximum number of bytes in the column
	private int _iCharOctetLength;
	
	//index of column in table (starting at 1)
	private int _iOrdinalPosition;
	
	//"NO" means column definitely does not allow NULL values; 
	//"YES" means the column might allow NULL values. An empty string means nobody knows.
	private String _sIsNullable;
	
	//catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
	private String _sScopeCatalog;
	
	//schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	private String _sScopeSchema;
	
	//table name that this the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	private String _sScopeTable;
	
	//source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types 
	//(null if DATA_TYPE isn't DISTINCT or user-generated REF) 
	private String _sSourceDataType;
	
	//True if the column is a primary-key of her table 
	private boolean _bPrimaryKey;

	
	public DbColumn(String tableCatalog, String tableSchema, String tableName,
			String columnName, int dataType, String typeName, int columnSize,
			int bufferLength, int decimalDigits, int numPrecRadix,
			int nullable, String remarks, String columnDef, int sqlDataType,
			int sqlDatetimeSub, int charOctetLength, int ordinalPosition,
			String isNullable, String scopeCatalog, String scopeSchema,
			String scopeTable, String sourceDataType, boolean primaryKey) {
		super();
		_sTableCatalog = tableCatalog;
		_sTableSchema = tableSchema;
		_sTableName = tableName;
		_sColumnName = columnName;
		_iDataType = dataType;
		_sTypeName = typeName;
		_iColumnSize = columnSize;
		_iBufferLength = bufferLength;
		_iDecimalDigits = decimalDigits;
		_iNumPrecRadix = numPrecRadix;
		_iNullable = nullable;
		_sRemarks = remarks;
		_sColumnDef = columnDef;
		_iSqlDataType = sqlDataType;
		_iSqlDatetimeSub = sqlDatetimeSub;
		_iCharOctetLength = charOctetLength;
		_iOrdinalPosition = ordinalPosition;
		_sIsNullable = isNullable;
		_sScopeCatalog = scopeCatalog;
		_sScopeSchema = scopeSchema;
		_sScopeTable = scopeTable;
		_sSourceDataType = sourceDataType;
		_bPrimaryKey = primaryKey;
	}

	public DbColumn() {
		new DbColumn(null,null,null,null,0,null,0,0,0,0,0,null,null,0,0,0,0,null,null,null,null,null,false);
	}

	/**
	 * May be null.
	 * @return String
	 */
	public String getTableCatalog() {
		return _sTableCatalog;
	}

	/**
	 * May be null.
	 * @param tableCatalog
	 */
	void setTableCatalog(String tableCatalog) {
		_sTableCatalog = tableCatalog;
	}

	/**
	 * May be null.
	 * @return String
	 */
	public String getTableSchema() {
		return _sTableSchema;
	}

	/**
	 * May be null.
	 * @param tableSchema
	 */
	void setTableSchema(String tableSchema) {
		_sTableSchema = tableSchema;
	}

	public String getTableName() {
		return _sTableName;
	}

	void setTableName(String tableName) {
		_sTableName = tableName;
	}

	public String getColumnName() {
		return _sColumnName;
	}

	void setColumnName(String columnName) {
		_sColumnName = columnName;
	}

	/**
	 * SQL type from java.sql.Types
	 * @return int
	 */
	public int getDataType() {
		return _iDataType;
	}

	/**
	 * SQL type from java.sql.Types
	 * @param dataType
	 */
	void setDataType(int dataType) {
		_iDataType = dataType;
	}

	/**
	 * Data source dependent type name, for a UDT the type name is fully qualified
	 * @return String
	 */
	public String getTypeName() {
		return _sTypeName;
	}

	/**
	 * Data source dependent type name, for a UDT the type name is fully qualified
	 * @param typeName
	 */
	void setTypeName(String typeName) {
		_sTypeName = typeName;
	}

	/**
	 * column size. For char or date types this is the maximum number of characters,
	 * for numeric or decimal types this is precision.
	 * @return int
	 */
	public int getColumnSize() {
		return _iColumnSize;
	}

	/**
	 * column size. For char or date types this is the maximum number of characters,
	 * for numeric or decimal types this is precision.
	 * @param columnSize
	 */
	void setColumnSize(int columnSize) {
		_iColumnSize = columnSize;
	}

	/**
	 * Is not used
	 * @return int
	 */
	public int getBufferLength() {
		return _iBufferLength;
	}

	/**
	 * Is not used
	 * @param bufferLength
	 */
	void setBufferLength(int bufferLength) {
		_iBufferLength = bufferLength;
	}

	/**
	 * The number of fractional digits
	 * @return int
	 */
	public int getDecimalDigits() {
		return _iDecimalDigits;
	}

	/**
	 * The number of fractional digits
	 * @param decimalDigits
	 */
	void setDecimalDigits(int decimalDigits) {
		_iDecimalDigits = decimalDigits;
	}

	/**
	 * Radix (typically either 10 or 2)
	 * @return int
	 */
	public int getNumPrecRadix() {
		return _iNumPrecRadix;
	}

	/**
	 * Radix (typically either 10 or 2)
	 * @param numPrecRadix
	 */
	void setNumPrecRadix(int numPrecRadix) {
		_iNumPrecRadix = numPrecRadix;
	}

	/**
	 * is NULL allowed. Values :
	 *    columnNoNulls - might not allow NULL values
	 *    columnNullable - definitely allows NULL values
	 *    columnNullableUnknown - nullability unknown 
	 * @return int
	 */
	public int getNullable() {
		return _iNullable;
	}

	/**
	 * is NULL allowed. Values :
	 *    columnNoNulls - might not allow NULL values
	 *    columnNullable - definitely allows NULL values
	 *    columnNullableUnknown - nullability unknown 
	 * @param nullable
	 */
	void setNullable(int nullable) {
		_iNullable = nullable;
	}
	
	/**
	 * Comment describing column
	 * May be null
	 * @return String
	 */
	public String getRemarks() {
		return _sRemarks;
	}

	/**
	 * Comment describing column
	 * May be null
	 * @param remarks
	 */
	void setRemarks(String remarks) {
		_sRemarks = remarks;
	}

	/**
	 * May be null.
	 * @return String
	 */
	public String getColumnDef() {
		return _sColumnDef;
	}

	/**
	 * May be null.
	 * @param columnDef
	 */
	void setColumnDef(String columnDef) {
		_sColumnDef = columnDef;
	}
	
	/**
	 * Unused;
	 * @return int
	 */
	public int getSqlDataType() {
		return _iSqlDataType;
	}

	/**
	 * Unused
	 * @param sqlDataType
	 */
	void setSqlDataType(int sqlDataType) {
		_iSqlDataType = sqlDataType;
	}

	/**
	 * Unused
	 * @return int
	 */
	public int getSqlDatetimeSub() {
		return _iSqlDatetimeSub;
	}

	/**
	 * Unused
	 * @param sqlDatetimeSub
	 */
	void setSqlDatetimeSub(int sqlDatetimeSub) {
		_iSqlDatetimeSub = sqlDatetimeSub;
	}
	
	/**
	 * for char types the maximum number of bytes in the column
	 * @return int
	 */
	public int getCharOctetLength() {
		return _iCharOctetLength;
	}

	/**
	 * for char types the maximum number of bytes in the column
	 * @param charOctetLength
	 */
	void setCharOctetLength(int charOctetLength) {
		_iCharOctetLength = charOctetLength;
	}

	/**
	 * index of column in table (starting at 1)
	 * @return int
	 */
	public int getOrdinalPosition() {
		return _iOrdinalPosition;
	}

	/**
	 * index of column in table (starting at 1)
	 * @param ordinalPosition
	 */
	void setOrdinalPosition(int ordinalPosition) {
		_iOrdinalPosition = ordinalPosition;
	}

	/**
	 * "NO" means column definitely does not allow NULL values; 
	 * "YES" means the column might allow NULL values. An empty string means nobody knows.
	 * @return String
	 */
	public String getIsNullable() {
		return _sIsNullable;
	}

	/**
	 * "NO" means column definitely does not allow NULL values; 
	 * "YES" means the column might allow NULL values. An empty string means nobody knows.
	 * @param isNullable
	 */
	void setIsNullable(String isNullable) {
		_sIsNullable = isNullable;
	}

	/**
	 * catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
	 * @return String
	 */
	public String getScopeCatalog() {
		return _sScopeCatalog;
	}

	/**
	 * catalog of table that is the scope of a reference attribute (null if DATA_TYPE isn't REF)
	 * @param scopeCatalog
	 */
	void setScopeCatalog(String scopeCatalog) {
		_sScopeCatalog = scopeCatalog;
	}

	/**
	 * schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	 * @return String
	 */
	public String getScopeSchema() {
		return _sScopeSchema;
	}

	/**
	 * schema of table that is the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	 * @param scopeSchema
	 */
	void setScopeSchema(String scopeSchema) {
		_sScopeSchema = scopeSchema;
	}

	/**
	 * table name that this the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	 * @return String
	 */
	public String getScopeTable() {
		return _sScopeTable;
	}

	/**
	 * table name that this the scope of a reference attribute (null if the DATA_TYPE isn't REF)
	 * @param scopeTable
	 */
	void setScopeTable(String scopeTable) {
		_sScopeTable = scopeTable;
	}

	/**
	 * source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types 
	 * (null if DATA_TYPE isn't DISTINCT or user-generated REF) 
	 * @return String
	 */
	public String getSourceDataType() {
		return _sSourceDataType;
	}

	/**
	 * source type of a distinct type or user-generated Ref type, SQL type from java.sql.Types 
	 * (null if DATA_TYPE isn't DISTINCT or user-generated REF) 
	 * @param sourceDataType
	 */
	void setSourceDataType(String sourceDataType) {
		_sSourceDataType = sourceDataType;
	}
	
	/**
	 * Return true if the column is a primary key of her table 
	 * @return boolean
	 */
	public boolean isPrimaryKey() {
		return _bPrimaryKey;
	}

	void setPrimaryKey(boolean primaryKey) {
		_bPrimaryKey = primaryKey;
	}

	public String toString() {
		return _sTableCatalog+"|"+_sTableSchema+"|"+_sTableName+"|"+_sColumnName+"|"+_iDataType+"|"+
			_sTypeName+"|"+_iColumnSize+"|"+_iBufferLength+"|"+_iDecimalDigits+"|"+_iNumPrecRadix+"|"+
			_iNullable+"|"+_sRemarks+"|"+_sColumnDef+"|"+_iSqlDataType+"|"+_iSqlDatetimeSub+"|"+
			_iCharOctetLength+"|"+_iOrdinalPosition+"|"+_sIsNullable+"|"+_sScopeCatalog+"|"+
			_sScopeSchema+"|"+_sScopeTable+"|"+_sSourceDataType+"|"+_bPrimaryKey;
	}

}
